package data.component;

import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.imgproc.Imgproc;
import utils.Matrix;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

public class Ceiling {
    private final ArrayList<Double[]> pts;
    public Double rate;
    public String id;
    public String code;

    public Ceiling(Double[] pt1, Double[] pt2, Double[] pt3, Double[] pt4, Double rate, String id) {
        this.pts = new ArrayList<>(Arrays.asList(pt1, pt2, pt3, pt4));
        this.rate = rate;
        this.id = id;
    }

    public void encode(Matrix m) {
        var points = pts.stream().map(p -> new Point(p[0], p[1])).collect(Collectors.toList());
        var mat = new MatOfPoint2f(points.get(0), points.get(1), points.get(2), points.get(3));
        var rRect = Imgproc.minAreaRect(mat);
        var x = rRect.center.x;
        var y = rRect.center.y;
        var w = rRect.size.width;
        var h = rRect.size.height;
        var theta = rRect.angle;
        this.code = Integer.toHexString((int) x / 10) + Integer.toHexString((int) y / 10) + Integer.toHexString((int) w / 10) +
                Integer.toHexString((int) h / 10) + Integer.toHexString((int) theta);

    }

}
